import React from 'react';
import router from 'umi/router';
import Link from 'umi/link'

import { connect } from 'dva';

import { Card, Divider, Button, Table,Modal } from 'antd';

import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;

const listPath = '/Sale/SaleOrders'
const lineFormPath = '/Sale/SaleOrderLineForm'
const formPath = '/Sale/SaleOrderForm'


@connect(({ saleOrder }) => ({ saleOrder }))
class Profile extends React.Component {
  state = {

    visibleDel: false,
    delMessage: '',
    visiblePost: false,
    visibleUnpost: false,

  }


  treeview = [
      {
        title: '',
        dataIndex: '_',
        render: (_, { id }) => {
          return <Link to={`${lineFormPath}?id=${id}`}>编辑</Link>;
        },
      },
      {
        title: '',
        dataIndex: '_1',
        render: (_, { id }) => {
          return <Button onClick={()=>this.delLine(id)}>删除</Button>
        },
      },
      { title: '摘要', dataIndex: 'name', },
      { title: '产品', dataIndex: 'product_id.name', },
      { title: '科目编码', dataIndex: 'account_id.code', },
      { title: '科目名称', dataIndex: 'account_id.name', },
      { title: '单价', dataIndex: 'price_unit', },
      { title: '数量', dataIndex: 'quantity', },
      { title: '折扣', dataIndex: 'discount', },
      { title: '不含税金额', dataIndex: 'price_subtotal', },
      { title: '含税金额', dataIndex: 'price_total', },
      { title: '税', dataIndex: 'price_tax', },
  ]


  taxTreeview = [
      /*{
        title: '',
        dataIndex: '_',
        render: (_, { id }) => {
          return <Link to={`/AccountInvoice/InvoiceTaxForm?id=${id}`}>编辑</Link>;
        },
      },*/
      { title: '摘要', dataIndex: 'name', },
      { title: '税', dataIndex: 'tax_id.name', },
      { title: '科目编码', dataIndex: 'account_id.code', },
      { title: '科目名称', dataIndex: 'account_id.name', },
      { title: '计税基础', dataIndex: 'base', },
      { title: '税金额', dataIndex: 'amount_total', },
  ]


  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'saleOrder/view',
        payload:  { id: this.id }
      });
    }
  }


  goto_createForm() {
    const { accountInvoice: { record } } = this.props;
    if(record.id){
      router.push(`${formPath}?id=${record.id}`);
    }
  }

  showDeleteMove() {
    this.setState({
      visibleDel: true
    })

  }

  showPostMove() {
    this.setState({
      visiblePost: true
    })

  }

  handleCancel(){
    this.setState({
      visibleDel: false,
      visiblePost: false,
      visibleUnpost: false,
    })
  }




  async computeTaxes( ){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'saleOrder/computeTaxes',
        payload:  {
          id: this.id
        }
      });

    }

  }

  async delLine( line_id ){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'saleOrder/write',
        payload:  {
          id: this.id ,
          vals: {invoice_line_ids: [[2,line_id,0]]}

        }
      });

    }

  }


  async delMove(){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'saleOrder/unlink',
        payload:  { id: this.id }
      });

      const { accountInvoice } = this.props;
      if( accountInvoice.id ){
        this.setState({
          delMessage: '不能删除该记录'
        })
      }
      else{
        router.push(listPath);

      }
    }
  }

  async postMove(){

    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'saleOrder/actionInvoiceOpen',
        payload:  { id:this.id }
      });

      this.setState({
        visiblePost: false
      })

    }
  }



  render() {
    const { saleOrder: { record } } = this.props;

    //console.log(record)

    const { visiblePost, //visibleUnpost,
      visibleDel,delMessage } = this.state

    return (
      <Card bordered={false}>

        <DescriptionList size="large" title='操作控制' style={{ marginBottom: 32 }}>
          <Description >
            <Button onClick={()=>this.goto_createForm()}>编辑</Button>
          </Description>
          <Description >
            <Button onClick={()=>this.showDeleteMove()}>删除</Button>
          </Description>
          <Description >
            <Button onClick={()=>this.computeTaxes()}>重新计算税</Button>
          </Description>


          <Description >
            <Button onClick={()=>this.showPostMove()}>过账</Button>
          </Description>

        </DescriptionList>

        <Modal title='删除'
          visible={visibleDel}
          onOk={()=>this.delMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认删除?
          {delMessage}

        </Modal>

        <Modal title='过账'
          visible={visiblePost}
          onOk={()=>this.postMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认过账?
        </Modal>


        <DescriptionList size="large" title='概要信息' style={{ marginBottom: 32 }}>
          <Description term='类型' >{record.type}</Description>
          <Description term='状态' >{record.state}</Description>
          <Description term='日期' >{record.date_order}</Description>
          <Description term='客户' >{record.partner_id && record.partner_id.name}</Description>
          <Description term='总金额' >{record.amount_total}</Description>
          <Description term='已付款/已核销' >{record.reconciled}</Description>
        </DescriptionList>

        <DescriptionList size="large" title='详细信息' style={{ marginBottom: 32 }}>
          <Description term='销售员' >{record.user_id && record.user_id.name}</Description>
          <Description term='发票地址' >{record.partner_invoice_id && record.partner_invoice_id.name}</Description>
          <Description term='送货地址' >{record.partner_shipping_id && record.partner_shipping_id.name}</Description>
          <Description term='价格表' >{record.pricelist_id && record.pricelist_id.name}</Description>
          <Description term='核算科目' >{record.analytic_account_id && record.analytic_account_id.name}</Description>

          <Description term='创建日期' >{record.create_date}</Description>
          <Description term='确认日期' >{record.confirmation_date}</Description>
          <Description term='有效日期' >{record.validity_date}</Description>
          <Description term='已过期' >{record.is_expired}</Description>
          <Description term='剩余有效天数' >{record.remaining_validity_days}</Description>

        </DescriptionList>

        <DescriptionList size="large" title='会计信息' style={{ marginBottom: 32 }}>
          <Description term='发票数' >{record.invoice_count}</Description>

          <Description term='账簿' >{record.journal_id && record.journal_id.name}</Description>
          <Description term='科目' >{record.account_id && record.account_id.name}</Description>
          <Description term='凭证号' >{record.move_id && record.move_id.name}</Description>
          <Description term='到期日期' >{record.date_due}</Description>
          <Description term='不含税金额' >{record.amount_untaxed}</Description>
          <Description term='税' >{record.amount_tax}</Description>
          <Description term='总金额' >{record.amount_total}</Description>
        </DescriptionList>
        <DescriptionList size="large" title='其他信息' style={{ marginBottom: 32 }}>
          <Description term='源单据' >{record.origin}</Description>
          <Description term='客户单据号' >{record.client_order_ref}</Description>
          <Description term='付款说明' >{record.reference}</Description>
          <Description term='备注' >{record.comment}</Description>
          <Description term='被冲发票' >{record.refund_invoice_id && record.refund_invoice_id.id}</Description>
          <Description term='反冲发票' >{
            record.refund_invoice_ids && JSON.stringify(record.refund_invoice_ids.map(item=>item.id))
          }</Description>
        </DescriptionList>
        <Divider style={{ marginBottom: 32 }} />
        <DescriptionList size="large" title='明细' style={{ marginBottom: 32 }}>
          <Link to={`/AccountInvoice/InvoiceLineForm?invoice_id=${record.id}`}>新增明细行</Link>
          <Table columns={this.treeview} dataSource={record.order_line} rowKey="id" />
        </DescriptionList>
        <DescriptionList size="large" title='发票明细' style={{ marginBottom: 32 }}>
          <Table columns={this.taxTreeview} dataSource={record.invoice_ids} rowKey="id" />
        </DescriptionList>
      </Card>
    );
  }
}

//

export default Profile;

